package com.bamboocloud.im.controller;

import com.bamboocloud.im.entity.Dept;
import com.bamboocloud.im.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther xuwen
 * @Date 2021/12/9 10:55
 * @Version 1.0
 */
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;
    @PostMapping("/dept/add")
    public boolean add(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }
    @GetMapping("/dept/get/{id}")
    public Dept get(@PathVariable("id") Long id){
        return deptService.selectDeptById(id);
    }
    @GetMapping("/dept/gets")
    public List<Dept> gets(){
        return deptService.selectAllDept();
    }
    @DeleteMapping("/dept/delete/{id}")
    public boolean delete(@PathVariable("id") Long id){
        return deptService.deleteDeptById(id);
    }
    @PutMapping("/dept/update")
    public Dept update(Dept dept){
        return deptService.UpdateDept(dept);
    }

//注册进来的微服务获得其一些信息
    @Autowired
    private DiscoveryClient discoveryClient;
    @GetMapping("/dept/discovery")
    public Object discovery(){
        //获取服务列表的清单
        List<String> services=discoveryClient.getServices();
        System.out.print("discovery=>services"+services);
        //得到一个具体的微服务信息
       List<ServiceInstance> instances= discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for(ServiceInstance instance:instances){
            System.out.print(
                    instance.getHost()+"/t"+
                            instance.getPort()+"/t"+
                            instance.getUri()+"/t"+
                            instance.getInstanceId()+"/t"
            );
        }
            return this.discoveryClient;
    }
}
